// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.standard.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.SystemConstants;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.enums.SecurityLevelEnum;
import com.liuxinlong.modules.dao.DepartmentDao;
import com.liuxinlong.modules.dao.ParameterDao;
import com.liuxinlong.modules.dao.SpecificationDao;
import com.liuxinlong.modules.entity.Specification;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.modules.standard.service.SpecificationService;
import com.liuxinlong.modules.system.service.FileManageService;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 设计规格书服务层实现类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年3月4日
 */
@Slf4j
@Service
public class SpecificationServiceImpl implements SpecificationService {

    @Autowired
    private SpecificationDao specificationDao;

    @Autowired
    private FileManageService fileManageService;

    @Autowired
    private ParameterDao parameterDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List<Map<String, Object>> pageSpecificationInfo(Map<String, Object> queryParam) {
        int startNum = (int) queryParam.get("startNum");
        List<Specification> originList = specificationDao.pageSpecificationList(queryParam);
        return completeResult(originList, startNum);
    }

    @Override
    public int getSpecificationCount(Map<String, Object> queryParam) {
        return specificationDao.getSpecificationCount(queryParam);
    }

    @Override
    public void addSpecificationInfo(Specification specification) {
        Specification dbInfo = specificationDao.getSpecificationByName(specification.getName());
        if (!ObjectUtils.isEmpty(dbInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "参数异常，规格书名称重复！");
        }
        String currentSn = specification.getSpecificationSn().substring(6);
        String id = SnowFlake.nextIdStr();
        specification.setId(id);
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        specification.setDepartment(currentUser.getDepartment());
        specification.setVersion("00");
        String attach = specification.getAttach();
        if (StringUtils.isNotEmpty(attach)) {
            fileManageService.updateBusinessFile(attach, id);
        }
        String userName = (String) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_ACCOUNT);
        specification.setCreateUser(userName);
        specificationDao.addSpecification(specification);
        parameterDao.updateParameterValue(SystemConstants.GENERATE_CODE_PARAM, currentSn);
    }

    @Override
    public void updateSpecificationInfo(Specification specification) {
        String id = specification.getId();
        if (StringUtils.isEmpty(id)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "参数异常，id不存在");
        }
        Specification oldInfo = specificationDao.getSpecificationById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "参数异常，原设计规格书不存在");
        }
        Specification dbInfo = specificationDao.getSpecificationByName(specification.getName());
        if (!ObjectUtils.isEmpty(dbInfo) && !StringUtils.equals(id, dbInfo.getId())) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "参数异常，规格书名称重复！");
        }
        String attach = specification.getAttach();
        fileManageService.updateBusinessFile(attach, id);
        String userName = (String) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_ACCOUNT);
        specification.setUpdateUser(userName);
        specificationDao.updateSpecification(specification);
    }

    @Override
    public void deleteSpecificationInfo(String id) {
        specificationDao.deleteSpecification(id);
    }

    /**
     * 补全数据
     *
     * @param originList 原始数据
     * @param startNum   起始数
     * @return 补全数据列表
     */
    private List<Map<String, Object>> completeResult(List<Specification> originList, int startNum) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (ObjectUtil.isEmpty(originList)) {
            return resultList;
        }
        Map<String, Map<String, String>> departmentMap = departmentDao.getDepartmentMap();
        int sort = startNum + 1;
        for (Specification item : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(item);
            map.put("sort", sort);
            if (StringUtils.isNotEmpty(item.getDepartment()) && departmentMap.containsKey(item.getDepartment())) {
                Map<String, String> departmentInfo = departmentMap.get(item.getDepartment());
                map.put("departmentName", departmentInfo.get("name"));
            }
            map.put("statusName", item.getStatus() == 1 ? "发布" : "草稿");
            map.put("securityLevelName", SecurityLevelEnum.getNameByValue(item.getSecurityLevel()));
            resultList.add(map);
            sort++;
        }
        return resultList;
    }

    @Override
    public void exportSpecificationInfo(String id) {
        // TODO Auto-generated method stub

    }
}
